package com.demo.stream_.collect;

import com.demo.stream_.Person;
import org.junit.Test;

import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author gy
 * @date 2022/5/11
 *
 * Collectors提供了一系列用于数据统计的静态方法：
 *
 * 计数：count
 * 平均值：averagingInt、averagingLong、averagingDouble
 * 最值：maxBy、minBy
 * 求和：summingInt、summingLong、summingDouble
 * 统计以上所有：summarizingInt、summarizingLong、summarizingDouble
 */
public class CountAveraging {
    @Test
    public void m1(){
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 23, "male", "New York"));
        personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 21, "female", "Washington"));

        // 求总数
        Long count = personList.stream().collect(Collectors.counting());
        // 求平均工资
        Double average = personList.stream().collect(Collectors.averagingDouble(Person::getSalary));
        // 求最高工资
        Optional<Integer> max = personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compareTo));
        // 求工资之和
        Integer sum = personList.stream().collect(Collectors.summingInt(Person::getSalary));
        // 一次性统计所有信息
        DoubleSummaryStatistics summaryStatistics = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary));

        System.out.println("summaryStatistics 员工的最大工资" + summaryStatistics.getMax());
        System.out.println("summaryStatistics 员工的最小工资" + summaryStatistics.getMin());
        System.out.println("summaryStatistics 员工的数量" + summaryStatistics.getCount());
        System.out.println("summaryStatistics 员工的工资总和" + summaryStatistics.getSum());
        System.out.println("summaryStatistics 员工的平均工资" + summaryStatistics.getAverage());

        System.out.println("员工总数：" + count);
        System.out.println("员工平均工资：" + average);
        System.out.println("员工工资总和：" + sum);
        System.out.println("员工工资所有统计：" + summaryStatistics);

    }
}
